package com.fly.data.jdbc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.fly.data.jdbc.dao.UserRepository;
import com.fly.data.jdbc.domain.User;
import com.fly.data.jdbc.service.UserService;

@Service
public class UserServiceImpl implements UserService
{
    @Autowired
    private UserRepository userRepository;
    
    @Override
    public void add()
    {
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 10; i++)
        {
            User user = new User();
            user.setUserName(RandomStringUtils.randomAlphabetic(10));
            user.setAge(RandomUtils.nextInt(10, 40));
            list.add(user);
        }
        userRepository.saveAll(list);
    }
    
    @Override
    public void delete(Long id)
    {
        userRepository.deleteById(id);
    }
    
    @Override
    public void save(User user)
    {
        userRepository.save(user);
    }
    
    @Override
    public User findById(Long id)
    {
        return userRepository.findById(id).get();
    }
    
    @Override
    public List<User> queryAll()
    {
        return userRepository.queryAll();
    }
    
    @Override
    public Page<User> findAll(Pageable pageable)
    {
        return userRepository.findAll(pageable);
    }
    
    @Override
    public List<User> findBy(Pageable pageable)
    {
        return userRepository.findBy(pageable);
    }
}
